using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using QLVMBAYDomain;
namespace QLVMBAYDatacess
{
    public class HangHangKhongDA : DataAccessBase
    {
		#region Constructors
		
		public HangHangKhongDA() : base()
        {
			_selectSql = "Select * From HANGHANGKHONG";
			_insertSql = "Insert Into HANGHANGKHONG(MAHANG,TENHANG) Values (@MAHANG,@TENHANG)";
        	_updateSql = "Update HANGHANGKHONG Set TENHANG=@TENHANG Where 1=1 And MAHANG=@MAHANG";
        	_deleteSql = "Delete From HANGHANGKHONG Where 1=1 And MAHANG=@MAHANG";
        	_primarSql = "Select * From HANGHANGKHONG Where 1=1 And MAHANG=@MAHANG";
        }
		
		protected static HangHangKhongDA _inst = null;
		public static HangHangKhongDA Inst
		{
			get
			{
				if(_inst == null)
					_inst = new HangHangKhongDA();
				return _inst;
			}
		}
		
		#endregion
		
        #region Get Methods

		private HangHangKhong GetCurrent(IDataReader reader, bool isDeep)
		{
			HangHangKhong ret = new HangHangKhong();
            ret.MaHang = (string)reader["MAHANG"];
            ret.TenHang = (string)reader["TENHANG"];
			
			
			return ret;
		}

		public List<HangHangKhong> GetAll(bool isDeep)
		{
			DbCommand comm = GetCommand(_selectSql);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<HangHangKhong> ret = new List<HangHangKhong>();
					while(dr.Read())
					{
						ret.Add(GetCurrent(dr, isDeep));
					}
					return ret;
                }
            }
		}
		
		public HangHangKhong GetByPrimaryKey(bool isDeep, params object[] args)
		{
			DbCommand cmd = GetCommand(_primarSql);
			if(args.Length < 1)
			{
				throw new Exception();
			}
            SetParam(cmd, "@MAHANG", args[0]);

            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
					if(dr.Read())
					{
						return GetCurrent(dr, isDeep);
					}
					return null;
                }
            }
		}
		
		public bool Insert(HangHangKhong entity)
		{
			DbCommand cmd = GetCommand(_insertSql);
            SetParam(cmd, "@MAHANG", entity.MaHang);
            SetParam(cmd, "@TENHANG", entity.TenHang);

			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Update(HangHangKhong entity)
		{
			DbCommand cmd = GetCommand(_updateSql);
            SetParam(cmd, "@MAHANG", entity.MaHang);
            SetParam(cmd, "@TENHANG", entity.TenHang);

			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Delete(HangHangKhong entity)
		{
			DbCommand cmd = GetCommand(_deleteSql);
            SetParam(cmd, "@MAHANG", entity.MaHang);

            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		#endregion
    }
}


